package com.wdress.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.DynaActionForm;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.classic.Session;
import org.hibernate.transform.Transformers;

import com.wdress.base.BaseAction;
import com.wdress.model.Wdress;
import com.wdress.model.Wkeyword;

public class CategorieAction extends BaseAction{

	@Override
	public String executeAction(DynaActionForm form, Session session,
			HttpServletRequest arg2, HttpServletResponse arg3,String lang) throws Exception {
		String categorie = form.getString("categorie");
		String color = form.getString("color");
		Query query = session.createQuery("from Wkeyword where types='categorie' and url=? and lang=?");
		query.setString(0, categorie);
		query.setString(1, lang);
		Wkeyword categorieObj= (Wkeyword) query.uniqueResult();
		arg2.setAttribute("categorieObj", categorieObj);
		query = session.createQuery("from Wkeyword where types='color' and url=? and lang=?");
		query.setString(0, color);
		query.setString(1, lang);
		Wkeyword colorObj= (Wkeyword) query.uniqueResult();
		arg2.setAttribute("categorieObj", categorieObj);
		arg2.setAttribute("colorObj", colorObj);
		if("all".equals(color)){
			query = session.createQuery("from Wdress where dress_type = ? and lang=?");
			query.setString(0, categorieObj.getKeyword());
			query.setString(1, lang);
		}else{
			query = session.createQuery("from Wdress where dress_type = ? and color= ? and lang=?");
			query.setString(0, categorieObj.getKeyword());
			query.setString(1, colorObj.getKeyword());
			query.setString(2, lang);
		}
		List<Wdress> dresslist=query.setMaxResults(24).list();
		arg2.setAttribute("dresslist", dresslist);
		SQLQuery sqlQuery = session.createSQLQuery("select b.keyword as color,b.url from wdress a,wkeyword b where a.color=b.keyword and a.lang=b.lang and a.dress_type=? and b.types ='color' and a.lang=? group by b.keyword having count(*)>0");
		sqlQuery.setString(0, categorieObj.getKeyword());
		sqlQuery.setString(1, lang);
		List colorlist = sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		arg2.setAttribute("colorlist", colorlist) ;
		query = session.createQuery("from Wkeyword where types='keyword' and lang=? order by mod(typeid,weekofyear(curdate())) ");
		query.setString(0, lang);
		List<Wkeyword> tagList= query.setMaxResults(10).list();
		arg2.setAttribute("tagList", tagList);
		initLangUrl(session, arg2, categorieObj.getId());
		return "success";
	}
	
	public void initLangUrl(Session session, HttpServletRequest arg2,Integer id){
		Query query = session.createQuery("from Wkeyword where types='categorie' and typeid=?");
		query.setInteger(0, 1);
		List<Wkeyword> urlObj= query.list();
		Wkeyword temp=null;
		for(int i=0;i<urlObj.size();i++){
			temp=urlObj.get(i);
			if("en".equals(temp.getLang())){
				arg2.setAttribute("enUrl", "http://www.superbdress.com/"+temp.getUrl()+"/all.html");
			}else{
				arg2.setAttribute(temp.getLang()+"Url", "http://"+temp.getLang()+".superbdress.com/"+temp.getUrl()+"/all.html");
			}
			
		}
	}

}
